package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.pojo.dto.UserFlashSaleDTO;
import cn.icanci.ladybird.pojo.entity.*;
import cn.icanci.ladybird.service.*;
import cn.icanci.ladybird.service.user.UserFlashSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: zxp
 * @date: Created in 2020/10/3 0003 下午 17:09
 * @classAction:秒杀
 */
@SuppressWarnings("all")
@Service
public class UserFlashSaleServiceImpl implements UserFlashSaleService {

    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private TicketDetailsTicketAddressMiddleService ticketDetailsTicketAddressMiddleService;
    @Autowired
    private TicketAddressService ticketAddressService;
    @Autowired
    private TicketDetailsTicketPriceMiddleService ticketDetailsTicketPriceMiddleService;
    @Autowired
    private TicketPriceService ticketPriceService;
    @Autowired
    private TicketDetailsTicketTimeMiddleService ticketDetailsTicketTimeMiddleService;
    @Autowired
    private TicketTimeService ticketTimeService;

    @Override
    public List<UserFlashSaleDTO> userFlashSaleDTO() {

        List<TicketDetails> ticketDetailsList = ticketDetailsService.queryByKill(0,5);

        // 需要封装的集合
        ArrayList<UserFlashSaleDTO> userFlashSaleDTOArrayList = new ArrayList<>();
        // 遍历查到的票
        for (TicketDetails details : ticketDetailsList) {
            // 创建DTO对象
            UserFlashSaleDTO userFlashSaleDTO= new UserFlashSaleDTO();
            // 拿到 id
            Long ticketDetailsId = details.getTicketDetailsId();
            // 设置DTO数据对象的id 商品名字 图片地址
            userFlashSaleDTO.setTicketDetailsId(ticketDetailsId);
            // 标题
            userFlashSaleDTO.setTicketDetailsInfo(details.getTicketDetailsInfo());
            // 图片的连接地址
            userFlashSaleDTO.setTicketDetailsTicketImgUrl(details.getTicketDetailsTicketImgUrl());
            //城市
            userFlashSaleDTO.setTicketAddressCity(details.getTicketAddressCity());
            //场馆
            userFlashSaleDTO.setTicketAddressVenue(details.getTicketAddressVenue());
            // 查询中间表对象

            // ticketDetailsId
            List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService
                    .queryByTicketDetailsId(ticketDetailsId);
            // 遍历集合
            ArrayList<Double> prices = new ArrayList<>();
            for (TicketDetailsTicketPriceMiddle middle : priceMiddles) {
                Long ticketPriceMiddleId = middle.getTicketPriceMiddleId();
                TicketPrice ticketPrice = ticketPriceService.queryById(ticketPriceMiddleId);
                prices.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
            }
            prices.sort((p1, p2) -> {
                return p1.compareTo(p2);
            });
            userFlashSaleDTO.setTicketPricePrice(prices.get(0));

            // 查询时间
            List<TicketDetailsTicketTimeMiddle> timeMiddles = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetailsId);
            // 遍历集合
            ArrayList<Date> dates = new ArrayList<>();
            for (TicketDetailsTicketTimeMiddle middle : timeMiddles) {
                Long timeMiddleId = middle.getTicketTimeMiddleId();
                TicketTime ticketTime = ticketTimeService.queryById(timeMiddleId);
                dates.add(ticketTime.getTicketTime());
            }
            dates.sort((d1, d2) -> {
                return d1.compareTo(d2);
            });
            userFlashSaleDTO.setTicketStartTime(dates.get(0));
            userFlashSaleDTO.setTicketEndTime(dates.get(dates.size() - 1));
            // 封装到结果对象集合
            userFlashSaleDTOArrayList.add(userFlashSaleDTO);
        }
        return userFlashSaleDTOArrayList;
    }
}
